-- round_kill_monsters
-- Created by panyl Nov/03/2016
-- 成就：一轮内击杀怪物

-- 内部函数声明
local roundKills;
local whenSkyCombat;

-- 关注一回合击杀事件
EventMgr.register("achievements/round_kill_monsters", event.ROUND_KILL_STAT, function(args)
    roundKills(args);
end);
EventMgr.register("achievements/round_kill_monsters", event.DUNGEON_SKY_COMBAT, function(args)
    whenSkyCombat(args);
end);

whenSkyCombat = function(args)
    local stat = {};
    local kill_list = args.kill_monster_list or {};

    for _, monster in pairs(kill_list) do
        base_id = monster:query("base_class_id") or monster.classId;
        stat[base_id] = (stat[base_id] or 0) + 1;
    end

    roundKills(stat);
end

-- 获得怪物死亡的回调
roundKills = function(args)
    if type(args) ~= "table" then
        return;
    end

    -- 玩家不在迷宫中
    if not DungeonM.isInDungeon() then
        return;
    end

    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("round_kill_monsters");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
            -- 不是该迷宫的成就
            return false;
        end

        -- 判断是否是需求怪物
        local config = AchievementM.query(id);
        local ruleArg = config["rule_arg"];
        local list = ruleArg["class_id"];

        if type(list) ~= "table" then
            return false;
        end

        for _, class_id in pairs(list) do
            if not args[class_id] then
                return false;
            end
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {

};
